package com.kkb.hbase.mr.demo1;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;

/**
 * Text, IntWritable   k2,v2  输入类型
 * Text, IntWritable   k3,v3  输出类型
 */
public class HxkReducer extends Reducer<Text, IntWritable,Text,IntWritable> {


    /**
     * 经过 分区 排序 规约 分组 之后到此处
     * Iterable是将相同的key的value进行了一次合并，形成了一个集合，然后调用reduce()
     * (hadoop,1) (hadoop,1) ==> hadoop,Iterable<IntWritable>(1,1)  -->  调用一次reduce()
     *  ...
     * @param key
     * @param values
     * @param context
     * @throws IOException
     * @throws InterruptedException
     */
    @Override
    protected void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
        int result = 0;
        for (IntWritable value: values){
            int i = value.get();  //把IntWritable变成int类型
            result += i;
        }
        //zai封装
        IntWritable intWritable = new IntWritable(result);
        //写数据到下游
        context.write(key,intWritable);
    }
}
